﻿// -----------------------------------------------------------------------
// <copyright file="CirclesDictionary.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex1
{
    using System;
    using System.IO;
    using System.Xml.Serialization;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using FacebookWrapper;
    using FacebookWrapper.ObjectModel;
    using Ex1.Logic;
    using Repository;
    
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CirclesOfFriends
    {
        public List<User> m_UsersByOneTypeCircle;

        public List<SerializableUserInCircuit> m_FrindsInGroups;

        public List<string> m_CirclesType;

        public CirclesOfFriends()
        {
            m_FrindsInGroups = new List<SerializableUserInCircuit>();
            m_CirclesType = new List<string>();
            m_UsersByOneTypeCircle = new List<User>();
        }

        public void SaveToXML(User i_LoggedinUser)
        {
            RepositoryFactory repository = new RepositoryFactory(i_LoggedinUser);
            repository.CreateFriendsInCircleRepository().SaveFriendsInCircle(m_FrindsInGroups);
            repository.CreateCircleRepository().SaveCircles(m_CirclesType);
        }

        public void LoadFromXML(User i_LoggedinUser)
        {
            List<SerializableUserInCircuit> friend;
            List<string> types;
            RepositoryFactory repository = new RepositoryFactory(i_LoggedinUser);
            friend = repository.CreateFriendsInCircleRepository().LoadFriendsInCircuits();
            if (friend != null)
            {
                m_FrindsInGroups = friend;
            }

            types = repository.CreateCircleRepository().LoadCircles();

            if (types != null)
            {
                m_CirclesType = types;
            }
        }

        public List<string> ChooseRandomProfilePhoto(User i_LoggedInUser)
        {
            List<string> randomPrifilePhotos = new List<string>();

            if (this.m_UsersByOneTypeCircle.Count > 0)
            {
                List<string> keys = new List<string>();

                foreach (User friend in this.m_UsersByOneTypeCircle)
                {
                    keys.Add(friend.Name);
                }

                Random random = new Random();
                int randomKey = random.Next(0, keys.Count);

                string selectedKey = keys[randomKey];
                User chossenUser = FriendsQuery.GetFriendsByName(selectedKey, i_LoggedInUser);
                randomPrifilePhotos.Add(chossenUser.PictureLargeURL);
            }

            return randomPrifilePhotos;
        }

        public void ConvertSerializableUserListInCircuitToUsersListByCircle(string i_NameOfCircuit, User i_LoggedInUser)
        {
            List<User> UsersByGroup = new List<User>();
            User userToAdd = null;
            foreach (SerializableUserInCircuit SerializableUser in m_FrindsInGroups)
            {
                 if (SerializableUser.CircuitName == i_NameOfCircuit)
                 {
                     userToAdd = convertFromsSerializableUserToUser(i_LoggedInUser, SerializableUser);
                     if (userToAdd != null)
                     {
                         UsersByGroup.Add(userToAdd);
                     }
                 }
             }

            m_UsersByOneTypeCircle = UsersByGroup;
        }

        ////from serializable to user
        private User convertFromsSerializableUserToUser(User i_LoggedInUser, SerializableUserInCircuit SerializableUser)
        {
            User convertedUser = null;

            foreach (User friend in i_LoggedInUser.Friends)
            {
                if (SerializableUser.Id == friend.Id)
                {
                    convertedUser = friend;
                    break;
                }
            }

            return convertedUser;
        }

        /// <summary>
        /// Delete by name of user
        /// </summary>
        /// <param name="i_UserName">The friend user's name</param>
        /// <param name="i_Circuit">The friend's circuit</param>
        public void DeleteFriendFromCircuit(string i_UserName, string i_Circuit)
        {
            foreach(SerializableUserInCircuit SerializableUser in this.m_FrindsInGroups)
            {
                if ((i_UserName == SerializableUser.Name) && (i_Circuit == SerializableUser.CircuitName))
                {
                    this.m_FrindsInGroups.Remove(SerializableUser);
                    break;
                }
            }
        }

        //// adding a new friend by circuit name 
        public void AddNewFriendToCircuit(User i_User, string i_Circuit)
        {
            SerializableUserInCircuit newFriend = new SerializableUserInCircuit();
            newFriend.CircuitName = i_Circuit;
            newFriend.Id = i_User.Id;
            newFriend.Name = i_User.Name;
            m_FrindsInGroups.Add(newFriend);
        }
    }
}
